package com.example.carproviderbackstage.service.impl.dl.admin;

import com.alibaba.dubbo.config.annotation.Service;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.example.carinterface.pojo.Driver;
import com.example.carinterface.pojo.Login;
import com.example.carinterface.pojo.User;
import com.example.carinterface.pojo.Worker;
import com.example.carinterface.service.dl.LoginService;
import com.example.carinterface.utils.MailUtils;

import com.example.carproviderbackstage.mapper.dl.admin.LoginMapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private LoginMapper loginMapper;


    @Override
    public Driver findDriver(String tel) {
        Driver driver = loginMapper.findDriver(tel);
        return driver;
    }

    @Override
    public User findUser(String tel) {
        return loginMapper.findUser(tel);
    }

    @Override
    public Login findLogin(String username) {
        return loginMapper.findLogin(username);
    }

    @Override
    public Worker findWorker(String username) {


        return loginMapper.findWorker(username);
    }

    @Override
    public boolean sendEmail(Map<String, String> map) {

        String to = map.get("email");
        String code = map.get("code");
        String text = "你的验证码为：【" + code + "】，有效时间5分钟。(测试期间：60分钟)";
        String title = "【car】邮箱验证";
        return MailUtils.sendMail(to, text, title);
    }

    @Override
    public boolean senSms(String tel, String templateCode, HashMap<String, String> param) {
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4GL9zqeDFvGDGCoHx85x", "WYCXo50Uly3LD7uB8aj7e0pzBuiQnc");
        IAcsClient client = new DefaultAcsClient(profile);

        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);
        request.setSysDomain("dysmsapi.aliyuncs.com");
        request.setSysVersion("2017-05-25");
        request.setSysAction("SendSms");

        //手机号，验证码，签名，摸板
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", tel);

        request.putQueryParameter("SignName", "ryou20940");

        request.putQueryParameter("TemplateCode", templateCode);
        //构建一个短信的验证码


        try {
            request.putQueryParameter("TemplateParam", new ObjectMapper().writeValueAsString(param));
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }


        try {
            CommonResponse response = client.getCommonResponse(request);
            System.out.println(response.getData());
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public int updateLoginPassword(Map<String, String> map) {

        return loginMapper.updateLoginPassword(map);
    }

    @Override
    public Login FindLoginByUsernameAndEmail(Map<String, String> map) {


        return loginMapper.FindLoginByUsernameAndEmail(map);
    }

    @Override
    public User addUserByTel(Map<String, String> map) {
        User noInfoUser = new User();
        noInfoUser.setTel(map.get("tel"));
        loginMapper.addUserByTel(map);

        return noInfoUser;
    }
}
